/*****************************************************
*
*               COPYRIGHT (c) 2015-2017 Hargic Corporation
*                         All Rights Reserved
*
* The source code contained or described herein and all documents
* related to the source code ("Material") are owned by Hargic
* Corporation or its licensors.  Title to the Material remains
* with Hargic Corporation or its suppliers and licensors.
*
* The Material is protected by worldwide copyright and trade secret
* laws and treaty provisions. No part of the Material may be used,
* copied, reproduced, modified, published, uploaded, posted, transmitted,
* distributed, or disclosed in any way except in accordance with the
* applicable license agreement.
*
* No license under any patent, copyright, trade secret or other
* intellectual property right is granted to or conferred upon you by
* disclosure or delivery of the Materials, either expressly, by
* implication, inducement, estoppel, except in accordance with the
* applicable license agreement.
*
* Unless otherwise agreed by Hargic in writing, you may not remove or
* alter this notice or any other notice embedded in Materials by Hargic
* or Hargic's suppliers or licensors in any way.
*
** Author: ted.huang
** Date: 2018-02-25
*
*****************************************************/

/***** Include files ********************************/
#include <bcm2835.h>
#include <stdio.h>

/***** Defines **************************************/

/***** Define structure *****************************/

/***** Define constant ******************************/

/***** Define global ********************************/

/***** Define Prototype of functions ****************/

/**
 * @brief
 * @param  None
 * @retval None
 */
int spi_init(int order, int mode, int divider)
{
    if (!bcm2835_spi_begin())
    {
        printf("bcm2835_spi_begin failed. Are you running as root??\n");
        return -1;
    }
    bcm2835_spi_setBitOrder(order);
    bcm2835_spi_setDataMode(mode);
    bcm2835_spi_setClockDivider(divider);
    // bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
    // bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);

    return 0;
}

int spi_deinit(void)
{
    bcm2835_spi_end();
    return 0;
}

uint8_t spi_transfer(uint8_t value)
{
    return bcm2835_spi_transfer(value);
}

void spi_transfern(char *buf, uint32_t len)
{
    bcm2835_spi_transfern(buf, len);
}

void spi_transfernb(char *tbuf, char *rbuf, uint32_t len)
{
    bcm2835_spi_transfernb(tbuf, rbuf, len);
}

/* End of file */



